package cn.iocoder.yudao.module.yyzx.controller.admin.nurser;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.nurser.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.nurser.NurserDO;
import cn.iocoder.yudao.module.yyzx.service.nurser.NurserService;

@Tag(name = "管理后台 - 护工")
@RestController
@RequestMapping("/yyzx/nurser")
@Validated
public class NurserController {

    @Resource
    private NurserService nurserService;

    @PostMapping("/create")
    @Operation(summary = "创建护工")
    @PreAuthorize("@ss.hasPermission('yyzx:nurser:create')")
    public CommonResult<Integer> createNurser(@Valid @RequestBody NurserSaveReqVO createReqVO) {
        return success(nurserService.createNurser(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新护工")
    @PreAuthorize("@ss.hasPermission('yyzx:nurser:update')")
    public CommonResult<Boolean> updateNurser(@Valid @RequestBody NurserSaveReqVO updateReqVO) {
        nurserService.updateNurser(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除护工")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:nurser:delete')")
    public CommonResult<Boolean> deleteNurser(@RequestParam("id") Integer id) {
        nurserService.deleteNurser(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得护工")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:nurser:query')")
    public CommonResult<NurserRespVO> getNurser(@RequestParam("id") Integer id) {
        NurserDO nurser = nurserService.getNurser(id);
        return success(BeanUtils.toBean(nurser, NurserRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得护工分页")
    @PreAuthorize("@ss.hasPermission('yyzx:nurser:query')")
    public CommonResult<PageResult<NurserRespVO>> getNurserPage(@Valid NurserPageReqVO pageReqVO) {
        PageResult<NurserDO> pageResult = nurserService.getNurserPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, NurserRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出护工 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:nurser:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportNurserExcel(@Valid NurserPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<NurserDO> list = nurserService.getNurserPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "护工.xls", "数据", NurserRespVO.class,
                        BeanUtils.toBean(list, NurserRespVO.class));
    }

}